package com.firewolf.spring.service.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

public class LogHandler implements InvocationHandler {

	private Object target;

	public LogHandler(Object target) {
		super();
		this.target = target;
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		System.out.println("日志，method " + method.getName() + " start with " + Arrays.toString(args));
		Object result = method.invoke(target, args);
		System.out.println("日志，method " + method.getName() + " end with " + result);
		return result;
	}

	/**
	 * 创建代理
	 * @param target
	 * @return
	 */
	public static Object createProxy(Object target) {
		return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(),
				new LogHandler(target));
	}
}
